﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace BlackStar
{
    public class CursorContentReader : ContentTypeReader<Cursor>
    {
        protected override Cursor Read(ContentReader input, Cursor existingInstance)
        {

            Cursor cursor = new Cursor();
            cursor.AssetName = input.ReadString();
            cursor.Offset = input.ReadObject<Point>();
            cursor.Load(input);
            return cursor;
        }
    }

    public class CursorListContentReader : ContentTypeReader<CursorList>
    {
        protected override CursorList Read(ContentReader input, CursorList existingInstance)
        {
            CursorList list = new CursorList();
            int count = input.ReadInt32();
            for (int i = 0; i < count; i++)
            { list.Add(input.ReadString(), input.ReadObject<Cursor>()); }
            if (!list.ContainsKey("Default"))
            { throw new ApplicationException("A Default cursort must be specified."); }
            return list;
        }
    }

    public class Cursor
    {
        public string AssetName;
        internal Texture2D texture;

        public Point Offset;

        internal void Load(ContentReader input)
        {
            string assetDir = input.AssetName.Substring(0, input.AssetName.LastIndexOf("\\") + 1);
            texture = input.ContentManager.Load<Texture2D>(assetDir + this.AssetName);
        }

        internal void Draw(SpriteBatch b, Point p, Color c)
        {
            b.Draw(texture, new Vector2(p.X - Offset.X, p.Y - Offset.Y), c);
        }
    }

    public class CursorList : Dictionary<string, Cursor>
    {
        public new Cursor this[string key]
        {
            get
            {
                if (this.ContainsKey(key))
                { return base[key]; }
                else
                { return base["Default"]; }
            }
            set { base[key] = value; }
        }
    }
}
